package com.qcby.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.qcby.entity.Student;
import com.qcby.entity.Teacher;

import com.qcby.service.StudentService;
import com.qcby.service.TeacherService;



@Controller


@RequestMapping("qjxt")
public class LoginController {
	
	@Autowired
	private StudentService studentService;
	public Student studentSgin;
	
	
	@RequestMapping("login_student")
	public String  login(@RequestParam("account") String user,@RequestParam("password") String password,Model model){
		Student student = new Student();
		student.setUser(user);
		student.setPassword(password);
		if(studentService.logincheck(student) != null){
			model.addAttribute("msg","登录成功");
			
			studentSgin = studentService.logincheck(student);
			System.out.println(studentSgin);
		}
	    else{
	    	model.addAttribute("msg","登录失败");
	    }
		
		return "login_student";
	}
	
	@RequestMapping("login_teacher")
	public String login2(HttpServletRequest request , HttpServletResponse response) throws IOException {

		String user =  request.getParameter("account");
		String password = request.getParameter("password");


		Teacher teacher = new Teacher();
		teacher.setUser(user);
		teacher.setPassword(password);
		if(teacherService.logincheck(teacher) != null) {
			response.getWriter().append("登录成功");
		}else {
			response.getWriter().append("登录失败");
		}
		
		return "login_teacher";
	}
	
	@RequestMapping("register_student")
	public String register (Student student, HttpServletRequest request) {
		
		System.out.println(student);
		
//		System.out.println(addNum);
		
		if(student.getUser().equals("") || student.getPassword().equals("") || student.getStudentName().equals("")) {
			request.setAttribute("msg", "注册失败");
			return "register_student";
		}else {
			Student sqlUser = studentService.selectSec(student);
			student.setStudentState("正常");
			student.setTime("无");
			student.setWhy("无");
			if(sqlUser == null) {
				int addNum = studentService.add(student);
				
				if(addNum == 1) {
					request.setAttribute("msg", "注册成功");
					return "register_student";
				}
			}else {
				request.setAttribute("msg", "已经存在");
				return "register_student";
			}
		}
		return null;
		
	}
	@Autowired
	private TeacherService teacherService;
	
	@RequestMapping("register_teacher")
	public String register2 (Teacher teacher, HttpServletRequest request) {
		
		System.out.println(teacher);
		if(teacher.getUser().equals("") || teacher.getPassword().equals("") || teacher.getTeacherName().equals("")) {
			request.setAttribute("msg", "注册失败");
			return "register_student";
		}else {
			Teacher sqlUser = teacherService.selectSec(teacher);
			
			if(sqlUser == null) {
				int addNum = teacherService.add(teacher);
				if(addNum == 1) {
					request.setAttribute("msg", "注册成功");
					return "register_teacher";
				}
			}else {
				request.setAttribute("msg", "已经存在");
				return "register_teacher";
			}
		}
		return null;
	}
	
	@RequestMapping("findStudent")
	public String findByStudent( Student student , Model model) {
		List<Student> data = studentService.findListByStudent(student);
		model.addAttribute("data",data);
		
		return "list";
	}
	@RequestMapping("findStudentSgin")
	public String findStudentSgin( Student student , Model model) {
		System.out.println(studentSgin);
		
		Student data = studentSgin;
		
		System.out.println(data);
		model.addAttribute("data",data);
		return "studentPage";
	}
	
	
	@RequestMapping("updata")
	public String update(Student student,Model model) {
		int addNum = studentService.update(student);
		System.out.println(addNum);
		List<Student> data = studentService.findListByStudent(student);
		model.addAttribute("data",data);
		
		return "list";
	}
	
	@RequestMapping("updataStudent")
	public String updataStudent(Student student,Model model) {
		System.out.println("########################");
		System.out.println(student);
		System.out.println("########################");
		int addNum = studentService.update(student);
		System.out.println(addNum);
		List<Student> data2 = studentService.findListByStudent(student);
		model.addAttribute("data2",data2);
		return "StudentShow";
	}
	
	
	/**
     * 分页
     *
     * @param currPage
     * @param model
     * @return
     */
	@RequestMapping("main")
    public String main(@RequestParam(value="currentPage",defaultValue="1",required=false)int currentPage,Model model){
        System.out.println(currentPage);
    	model.addAttribute("pagemsg", studentService.findByPage(currentPage));//回显分页数据

		return "list";
    }
	
}
